﻿#region

using System;
using System.Data;
using System.Xml;

#endregion

namespace ZhhbSoft.Data.Criterion
{
    /// <summary>
    ///Data 的摘要说明
    /// </summary>
    public class Item : Node
    {
        public string ColumnName
        {
            get { return Attributes["columnName"]; }
            set { Attributes["columnName"] = value; }
        }
        public string Operator
        {
            get { return Attributes["operator"]; }
            set { Attributes["operator"] = value; }

        }
        public string ReferenceValue
        {
            get { return _text; }
            set { _text = value; }
        }
        public string DataType
        {
            get { return Attributes["dataType"]; }
            set { Attributes["dataType"] = value; }
        }

        public Item(string columnName, string @operator, string referenceValue, DbType dbType)
            : this(columnName, @operator, referenceValue, dbType, "")
        {
        }

        public Item(string columnName, string @operator, string referenceValue, DbType dbType, string joinType)
            : this(columnName, @operator, referenceValue, dbType.ToString(), joinType)
        {

        }

        public Item(string columnName, string @operator, string referenceValue, string dataType)
            : this(columnName, @operator, referenceValue, dataType, "")
        {
        }

        public Item(string columnName, string @operator, string referenceValue, string dataType, string joinType)
            : base("item")
        {
            _childs = new NodeCollection();
            Attributes["columnName"] = columnName;
            Attributes["operator"] = @operator;
            Attributes["dataType"] = dataType;
            Attributes["joinType"] = joinType;
            if (joinType == "left")
            {
                _text = "%" + referenceValue;
            }
            else if (joinType == "right")
            {
                _text = referenceValue + "%";
            }
            else if (joinType == "both")
            {
                _text = "%" + referenceValue + "%";
            }
            else
            {
                _text = referenceValue;
            }
        }

        public override void FillFromXML(XmlNode xmlNode)
        {
            try
            {
                Attributes["columnName"] = xmlNode.Attributes["columnName"].Value;
                Attributes["operator"] = xmlNode.Attributes["operator"].Value;
                _text = xmlNode.InnerText;
            }
            catch (Exception)
            {

            }
        }
        public override string ToString()
        {
            string strTemp = string.Empty;
            if (Attributes["dataType"] == "Object")
            {
                if (Attributes["columnName"] != "")
                {
                    strTemp = Attributes["columnName"] + "." + Attributes["operator"].Replace("@param", _text.Replace("'", "''"));
                }
                else
                {
                    strTemp = Attributes["operator"].Replace("@param", _text.Replace("'", "''"));
                }
            }
            else if (Attributes["dataType"] == "String")
            {
                strTemp = Attributes["columnName"] + " " + Attributes["operator"] + " '" + _text.Replace("'", "''") + "'";
            }
            else if (Attributes["dataType"] == "DateTime")
            {
                try
                {
                    strTemp = Attributes["columnName"] + " " + Attributes["operator"] + " '" + Convert.ToDateTime(_text) + "'";
                }
                catch (Exception)
                {
                    throw new Exception("输入值的格式有误，[" + Attributes["columnName"] + "]需要日期格式！");
                }
            }
            else if (Attributes["dataType"] == "Time")
            {
                try
                {
                    strTemp = Attributes["columnName"] + " " + Attributes["operator"] + " '" + (Convert.ToDateTime(_text.Replace(";", ":")) - DateTime.Now.Date) + "'";
                }
                catch (Exception)
                {
                    throw new Exception("输入值的格式有误，[" + Attributes["columnName"] + "]需要日期格式！");
                }
            }
            else if (Attributes["dataType"] == "Int32")
            {
                try
                {
                    strTemp = Attributes["columnName"] + " " + Attributes["operator"] + " " + Convert.ToInt32(_text);
                }
                catch (Exception)
                {
                    throw new Exception("输入值的格式有误，[" + Attributes["columnName"] + "]需要整数格式！");
                }
            }
            else if (Attributes["dataType"] == "Int64")
            {
                try
                {
                    strTemp = Attributes["columnName"] + " " + Attributes["operator"] + " " + Convert.ToInt64(_text);
                }
                catch (Exception)
                {
                    throw new Exception("输入值的格式有误，[" + Attributes["columnName"] + "]需要长整数格式！");
                }
            }
            else if (Attributes["dataType"] == "Single")
            {
                try
                {
                    strTemp = Attributes["columnName"] + " " + Attributes["operator"] + " " + Convert.ToSingle(_text);
                }
                catch (Exception)
                {
                    throw new Exception("输入值的格式有误，[" + Attributes["columnName"] + "]需要实数格式！");
                }
            }
            else if (Attributes["dataType"] == "Boolean")
            {
                try
                {
                    strTemp = Attributes["columnName"] + " " + Attributes["operator"] + " " + (Convert.ToBoolean(_text) ? "1" : "0") + "";
                }
                catch (Exception)
                {
                    try
                    {
                        strTemp = Attributes["columnName"] + " " + Attributes["operator"] + " " +
                                  (Convert.ToInt32(_text) != 0 ? "1" : "0") + "";
                    }
                    catch (Exception)
                    {
                        throw new Exception("输入值的格式有误，[" + Attributes["columnName"] + "]需要布尔类型！");
                    }
                }
            }
            else if (Attributes["dataType"] == "Xml")
            {
                try
                {
                    strTemp = Attributes["columnName"] + "." + Attributes["operator"].Replace("@param", _text.Replace("'", "''"));
                }
                catch (Exception)
                {
                }
            }
            else
            {
                strTemp += "'" + _text.Replace("'", "''") + "'";
            }
            return strTemp;
        }
    }
}